package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public void add(Setmeal setmeal) {
        //添加setmeal并返回setmeal的id
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        //获取checkgroupIds
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        setSetmealIdAndCheckgroupIdBatch(setmealId, checkgroupIds);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Setmeal> setmeals = setmealDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), setmeals);
    }

    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmeals = setmealDao.getSetmeal();
        return setmeals;
    }

    @Override
    //通过循环多次从数据库拿到数据，每次都从数据库拿，需要多次连接到数据库，不好
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.getSetmealById(id);
        List<CheckGroup> checkGroups = setmealDao.findCheckGroupBySetmealId(id);
        for (CheckGroup checkGroup : checkGroups) {
            List<CheckItem> checkItems = setmealDao.findCheckItemByCheckGroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }

    @Override
    //所以一次性从数据库拿到需要的数据，在service层进行循环取值
    public Setmeal findByIdBatch(Integer id) {
        Setmeal setmeal = setmealDao.getSetmealById(id);
        /*List<Integer> checkGroupIds = setmealDao.findCheckGroupIdsBySetmealId(id);*/
        List<CheckGroup> checkGroups = setmealDao.findCheckGroupBySetmealId(id);

        List<Integer> checkGroupIds=getCheckGroupIds(checkGroups);


        //套餐下面所有检查组的检查项
        List<CheckItem> checkItems = setmealDao.findCheckItemBatchByCheckGroupIds(checkGroupIds);

        //自动分组
        Map<Integer,List<CheckItem>> collect =checkItems.stream().
                collect(Collectors.groupingBy(CheckItem::getCheckgroupId));
        for (CheckGroup checkGroup : checkGroups) {
            checkGroup.setCheckItems(collect.get(checkGroup.getId()));
        }

        //手动分组2
        /*Map<Integer,List<CheckItem>> collect2=new HashMap<>();
        for (CheckItem checkItem : checkItems) {
            List<CheckItem> checkItemMap=collect2.get(checkItem.getCheckgroupId());
            if (checkItemMap==null){
                checkItemMap=new ArrayList<>();
                collect2.put(checkItem.getCheckgroupId(),checkItemMap);
            }
            checkItemMap.add(checkItem);
        }

        for (CheckGroup checkGroup : checkGroups) {
            checkGroup.setCheckItems(collect2.get(checkGroup.getId()));
        }
*/
        //手动分组
        /*for (CheckGroup checkGroup : checkGroups) {
            List<CheckItem> checkItemList=new ArrayList<>();
            for (CheckItem checkItem : checkItems) {
                if (checkGroup.getId()==checkItem.getCheckgroupId()){
                    checkItemList.add(checkItem);
                }
            }
            checkGroup.setCheckItems(checkItemList);
        }*/
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }

    @Override
    public Setmeal findDetailById(Integer id) {
        Setmeal setmeal = setmealDao.getSetmealById(id);
        return setmeal;
    }

    private List<Integer> getCheckGroupIds(List<CheckGroup> checkGroups) {
        List<Integer> ids=new ArrayList<>();
        if (CollectionUtil.isNotEmpty(checkGroups)){
            for (CheckGroup checkGroup : checkGroups) {
                ids.add(checkGroup.getId());
            }
        }
        return ids;
    }

    private void setSetmealIdAndCheckgroupIdBatch(Integer setmealId, List<Integer> checkgroupIds) {
        //添加setmeal的id和checkgroup的id到数据库中（批量插入）
        if (null != checkgroupIds && checkgroupIds.size() > 0) {
            List<Map> batchParam = new ArrayList<>();
            for (Integer checkgroupId : checkgroupIds) {
                Map map = new HashMap();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                batchParam.add(map);
            }
            //通过dao层添加到数据库
            setmealDao.setSetmealIdAndCheckgroupIdBatch(batchParam);
        }
    }


}
